home *** CD-ROM | disk | FTP | other *** search
/ Games of Daze / Infomagic - Games of Daze (Summer 1995) (Disc 1 of 2).iso / x2ftp / msdos / libs / knowhow4 / mouse.cpp < prev    next >
C/C++ Source or Header  |  1994-10-10  |  7KB  |  374 lines

  1. /*
  2.  
  3.    ATTENTION! If compiling in tiny, small or medium memory model,
  4.    use   Options | Compiler | Code Generation | Assume SS Equals DS never
  5.    settings !
  6.  
  7. */
  8. #include <graphics.h>
  9. #include "graphpp.h"
  10. // #include "graph.h"
  11.  
  12. #ifndef __GEOM
  13. #include "geom.h"
  14. #endif
  15.  
  16. #include "mouse.h"
  17.  
  18. // Virtual window management
  19.  
  20. static mousemanager MouseManager;
  21. mousemanager *  mousehandler::manager=& MouseManager;
  22. int mousemanager::Nbuttons=0;
  23.  
  24.  //
  25.  // *************************************************
  26.  //
  27.  static int driverInstalled=0;
  28.  
  29.  static int mouseInit() // returns # of buttons , not user-available
  30.             //  user should call mouseReset
  31.     {    driverInstalled=1;
  32.       asm{
  33.       xor ax,ax
  34.       mov es,ax
  35.       mov bx,(33h*4)
  36.       mov  ax,es:[bx]
  37.       or   ax,es:[bx+2]
  38.       jz   no_driver
  39.       mov ax,0
  40.       int 33h
  41.       or ax,ax
  42.       jz no_driver
  43.       }
  44.     return _BX;
  45.     no_driver:
  46.     driverInstalled=0;
  47.     return 0;
  48.     }
  49.  
  50.  int mouseType() // 0 - no mouse , 2,3
  51.     {return mousemanager::Nbuttons;}
  52.  
  53.  void mouseReset()
  54.     {
  55.  
  56.       if(!driverInstalled) return;
  57.      asm{
  58.       mov ax,0
  59.       int 33h
  60.       }
  61.       MouseManager. reset();
  62.     }
  63.  
  64. mousestatus mouseGetStatus()
  65.      {    mousestatus s;
  66.  
  67.      if (driverInstalled)
  68.       {
  69.       asm {
  70.          mov ax,3
  71.          int 33h
  72.        }
  73.        s.buttonstate=_BX;
  74.        s.x=_CX;
  75.        s.y=_DX;
  76.       }
  77.       else
  78.       {
  79.        s.buttonstate=0;
  80.        s.x=0;
  81.        s.y=0;
  82.  
  83.  
  84.       }
  85.     return s;
  86.      }
  87.  
  88. void mouseSetPosition(loc at)
  89.    {
  90.       if(!driverInstalled) return;
  91.       _CX=at.X;
  92.       _DX=at.Y;
  93.       asm{
  94.     mov ax,4
  95.     int 33h
  96.  
  97.       }
  98.  
  99.    }
  100.  
  101.   void mouseSetHorizontalRange(int xmin,int xmax)
  102.      {
  103.       if(!driverInstalled) return;
  104.  
  105.        asm{
  106.         mov ax,7
  107.         mov cx,xmin
  108.         mov dx,xmax
  109.         int 33h
  110.        }
  111.      }
  112.  
  113.   void mouseSetVerticalRange(int ymin,int ymax)
  114.      {
  115.       if(!driverInstalled) return;
  116.        asm{
  117.         mov ax,8
  118.         mov cx,ymin
  119.         mov dx,ymax
  120.         int 33h
  121.        }
  122.      }
  123.  
  124.     void mouseSetRange(const rect& range)
  125.       {
  126.      mouseSetHorizontalRange(range.left(),range.right());
  127.      mouseSetVerticalRange(range.top(),range.bottom());
  128.       }
  129.  
  130.    void mouseDefaultRange() // Graphic-mode only !
  131.     {
  132.        mouseSetRange(rect(loc(),screensize()));
  133.     }
  134.  
  135.    void mouseSetSpeed(const loc& speed) // mickeys / 8 pixels
  136.      {
  137.       if(!driverInstalled) return;
  138.        _CX=speed.X;
  139.        _DX=speed.Y;
  140.        asm{
  141.        mov ax,15
  142.        int 33h
  143.        }
  144.      }
  145.  
  146.   void mouseDefaultSpeed()
  147.      {
  148.  
  149.        mouseSetSpeed(loc(8,16));
  150.      }
  151.  
  152.  
  153.    void mouseSetDoublingSpeed(int speed) //  mickeys /second
  154.      {
  155.       if(!driverInstalled) return;
  156.        asm{
  157.         mov ax,19
  158.         mov dx,speed
  159.         int 33h
  160.        }
  161.      }
  162.  
  163.    void mouseDefaultDoublingSpeed()
  164.     {
  165.  
  166.     mouseSetDoublingSpeed(64);
  167.     }
  168.  
  169.    void mouseDoublingOff()
  170.     {
  171.       mouseSetDoublingSpeed(34000);
  172.     }
  173.  
  174.    unsigned mouseStateStorageSize()
  175.     {
  176.       if(!driverInstalled) return 2;
  177.  
  178.     _AX=21;
  179.     asm int 33h;
  180.     return _BX;
  181.     }
  182.  
  183.    void mouseSaveState(char far * buffer)
  184.     {
  185.       if(!driverInstalled) return ;
  186.  
  187.        asm{
  188.        mov ax,22
  189.        les dx, buffer
  190.        int 33h
  191.  
  192.        }
  193.     }
  194.  
  195.    void mouseRestoreState(char far * buffer)
  196.     {
  197.       if(!driverInstalled) return ;
  198.        asm{
  199.        mov ax,23
  200.        les dx, buffer
  201.        int 33h
  202.        }
  203.     }
  204.  
  205.  
  206.   void mouseSetHandler (mousehandlerfunc handler)
  207.      {
  208.       if(!driverInstalled) return ;
  209.  
  210.      _CX=mouseeventlow::MSALL;
  211.  
  212.       asm{
  213.        mov ax,12
  214.        les dx,handler
  215.        int 33h
  216.      }
  217.      }
  218.  
  219.  
  220.   void mouseDisableHandler ()
  221.      {
  222.       if(!driverInstalled) return ;
  223.  
  224.        _CX=mouseeventlow::MSNONE;
  225.       asm{
  226.        mov ax,12
  227.        xor dx,dx
  228.        mov es,dx
  229.        int 33h
  230.     }
  231.      }
  232.  
  233.  
  234.    void mouseShowCursor()
  235.       {
  236.       if(!driverInstalled) return ;
  237.  
  238.      asm{
  239.           mov ax,1
  240.           int 33h
  241.      }
  242.       }
  243.  
  244.    void mouseHideCursor()
  245.       {
  246.       if(!driverInstalled) return ;
  247.      asm{
  248.           mov ax,2
  249.           int 33h
  250.      }
  251.       }
  252.  
  253.  
  254. void mouseSuspend()
  255.      {
  256.        mouseHideCursor();
  257.        mouseDisableHandler();
  258.      }
  259.  
  260. void mouseResume()
  261.     {
  262.        mouseShowCursor();
  263.        MouseManager.reset();
  264.     }
  265.  
  266.  //
  267.  // *************************************************
  268.  //
  269.  
  270.  
  271.       mousehandler::mousehandler(unsigned callmask,rect * area):
  272.      EventMask(callmask),domain(0)
  273.     {
  274.       if (area) domain= new rect(*area);
  275.       manager->attach(this);
  276.     }
  277.  
  278.       mousehandler::~mousehandler()
  279.     {
  280.       manager->detach(this);
  281.       if( domain) delete domain;
  282.     }
  283.  
  284.  
  285.  
  286.  
  287.    void huge _saveregs  mousemanager::mainmousehandler()
  288.    {
  289.       mouseeventlow e;
  290.       e.evtype=_AX;e.buttonstatus=_BX;e.x=_CX;e.y=_DX;e.mx=_SI;e.my=_DI;
  291.       mousehandler * list=mousehandler::manager -> handlist;
  292.       mousehandler * ip;
  293.  
  294.       loc point(e.x,e.y);
  295.  
  296.        
  297.       if(mousehandler::manager ->locked) return;
  298.        // Just ignore mouse event.
  299.        // more accurate action would be put this event in pending state,
  300.        // and then unlock() procedure should call handler again
  301.        // (in this case handler should consist of 2 parts:
  302.        // interrupt & strategy)
  303.  
  304.       for(ip=list;ip && e.evtype ;ip=ip->next)
  305.     if ((ip->EventMask & e.evtype) && ip->covers (point) )
  306.         ip->handle(e);
  307.  
  308.  
  309.  
  310.    }
  311.  
  312. mousemanager::mousemanager() :handlist(0),locked(0)
  313.   {
  314.     Nbuttons=mouseInit();
  315.     mouseSetHandler(mousemanager::mainmousehandler);
  316.   }
  317.  
  318.  void mousemanager::reset()
  319.  {
  320.      mouseSetHandler(mousemanager::mainmousehandler);
  321.  }
  322.  
  323.  
  324.  
  325. void mousemanager::attach ( mousehandler * handler)
  326.   {
  327.     locked=1;
  328.     handler->next= handlist;
  329.     handlist=handler;
  330.     locked=0;
  331.   }
  332.  
  333. void mousemanager::detach (mousehandler * handler)
  334.   {  mousehandler * ip, * prev;
  335.  
  336.      for (prev=0,ip=handlist;ip && (ip != handler);prev=ip,ip=ip->next)
  337.     ; // just look through
  338.      if (ip==0) return; // Not found !
  339.      locked=1;
  340.      if (prev==0) handlist=handlist->next; // remove head
  341.     else  prev->next=ip->next;         // remove inside chain
  342.      locked=0;
  343.  
  344.   }
  345.  
  346. mousemanager::~mousemanager()
  347.   {
  348.     mouseDisableHandler();
  349.   }
  350.  
  351. int mousehole::covers(loc& pos) const
  352.   {
  353.     return  door.contains(pos);
  354.   }
  355.  
  356. void mousehole::handle(mouseeventlow& ev)
  357.  {
  358.    loc current(ev.x,ev.y);
  359.    if(isinside)
  360.      {
  361.       if (!covers(current)) {isinside=0;leave();}
  362.      }
  363.    else
  364.     {
  365.      if (covers(current)) {isinside=1;enter();}
  366.     }
  367.  }
  368.  
  369. mousehole::mousehole(rect hole):
  370.        mousehandler(mouseeventlow::MSMOVE),door(hole),isinside(0)
  371.        { }
  372.  
  373.  
  374.